#include "ImageProcessingWidget.h"
#include "ui_ImageProcessingWidget.h"
#include "ImageAlgorithmButton.h"
#include <QDebug>
#include <QVBoxLayout>

ImageProcessingWidget::ImageProcessingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ImageProcessingWidget),
    m_buttonGroup(new QButtonGroup(this))
{
    ui->setupUi(this);
    
    // 设置按钮组为互斥模式
    m_buttonGroup->setExclusive(true);
}

ImageProcessingWidget::~ImageProcessingWidget()
{
    delete ui;
}

void ImageProcessingWidget::onAlgorithmWidgetReady(const QString &algorithmName, QWidget *widget, const QString &iconPath)
{
    // 创建图像算法按钮
    ImageAlgorithmButton *button = new ImageAlgorithmButton(ui->scrollAreaWidgetContents);
    button->setText(algorithmName);
    button->setIcon(iconPath);
    ui->verticalLayout->insertWidget(ui->verticalLayout->count()-1,button);
    // 将按钮添加到按钮组
    m_buttonGroup->addButton(button);
    
    // 存储按钮
    m_algorithmButtons[algorithmName] = button;
    
    // 存储widget
    m_algorithmWidgets[algorithmName] = widget;
    
    // 添加widget到布局中
    if (widget) {
        QVBoxLayout *layout = qobject_cast<QVBoxLayout*>(ui->scrollAreaWidgetContents->layout());
        if (layout) {
            // 将widget添加到按钮下方
            int buttonIndex = layout->indexOf(button);
            if (buttonIndex >= 0) {
                layout->insertWidget(buttonIndex + 1, widget);
                widget->hide();  // 初始隐藏
            }
        }
    }
    else
    {
        button->setHasSettingsWidget(false);
    }
    
    // 连接按钮的toggled信号
    connect(button, &QPushButton::toggled, this, [=](bool checked) {
        if (widget)
        {
            // 根据按钮状态显示或隐藏对应的widget
            widget->setVisible(checked);
            qDebug()<<"Checkde:"<<checked<<"WidgetVisible:"<<widget->isVisible();
        }
        if (checked)
        {
            emit processImageRequested(algorithmName);
        }
    });
    
    // 设置按钮初始状态为禁用
    button->setDisabled(true);
}

void ImageProcessingWidget::selectAlgorithmButton(const QString &algorithmName)
{
    if (m_algorithmButtons.contains(algorithmName)) {
        ImageAlgorithmButton *button = m_algorithmButtons[algorithmName];
        
        // 选中按钮但不触发信号
        button->blockSignals(true);
        button->setChecked(true);
        button->blockSignals(false);
        
        // 显示对应的widget
        if (m_algorithmWidgets.contains(algorithmName)) {
            m_algorithmWidgets[algorithmName]->show();
        }
    }
}

void ImageProcessingWidget::enableAllAlgorithmButtons(bool enable)
{
    // 遍历所有算法按钮并设置启用/禁用状态
    for (auto it = m_algorithmButtons.constBegin(); it != m_algorithmButtons.constEnd(); ++it) {
        ImageAlgorithmButton *button = it.value();
        if (button) {
            button->setEnabled(enable);
        }
    }
}

void ImageProcessingWidget::setIsProcessed(const QString &algorithmName, bool isProcessed)
{
    if(!m_algorithmButtons.contains(algorithmName))
    {
        return;
    }
    m_algorithmButtons[algorithmName]->setProcessed(isProcessed);
}

void ImageProcessingWidget::resetAllButtonsState()
{
    m_buttonGroup->setExclusive(false);
    // 遍历所有算法按钮
    for (auto it = m_algorithmButtons.constBegin(); it != m_algorithmButtons.constEnd(); ++it)
    {
        ImageAlgorithmButton *button = it.value();
        if (button && button->isChecked()) // 只处理已选中的按钮
        {
            button->setChecked(false);
        }
        qDebug()<<button->isChecked();
    }
    m_buttonGroup->setExclusive(true);
}
